﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhysicalDataProvider;
using System.Data.Common;
using DTOFrameworks;
using FooFun.DataElements.Dtos;

namespace FooFun.Faq.Dal.Services
{
    public class TopicAboutUserService : ITopicService, IReplyService
    {
        #region ITopicService 成员

        public void CreateTopic(string topicTitle, string remark, Guid eventId, Guid forumId, Guid userId)
        {
            using (DbConnection conn = SqlHelper.Connection)
            {
                string sql = @"INSERT INTO t_TopicsAboutUser
(UserId,TopicId,Status)
VALUES
(?,?,?)";
                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                DbParameter para1 = cmd.CreateParameter();
                para1.Value = DataProviderUtil.GetGuidBase64(userId);
                cmd.Parameters.Add(para1);

                DbParameter para2 = cmd.CreateParameter();
                para2.Value = DataProviderUtil.GetGuidBase64(eventId);
                cmd.Parameters.Add(para2);

                DbParameter para3 = cmd.CreateParameter();
                para3.Value = (int)FAQActionEnum.AddTopic;
                cmd.Parameters.Add(para3);

                conn.Open();

                cmd.ExecuteNonQuery();

                conn.Close();
            }
        }

        public void MoveTopic(FooFun.DataElements.Dtos.IDataElement topic, Guid forumId)
        {
            return;
        }

        public void RemoveTopic(Guid topicId)
        {
            return;
        }

        #endregion

        #region IReplyService 成员

        public void AddReply(IDataElement topic, Guid eventId, Guid userId, string reply)
        {
            using (DbConnection conn = SqlHelper.Connection)
            {
                string sql = @"SELECT Count(*) FROM t_TopicsAboutUser
WHERE UserId=? AND TopicId=? AND Status=?";

                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;

                DbParameter para1 = cmd.CreateParameter();
                para1.Value = DataProviderUtil.GetGuidBase64(userId);
                cmd.Parameters.Add(para1);

                DbParameter para2 = cmd.CreateParameter();
                para2.Value
                    = DataProviderUtil.GetGuidBase64(topic["@TopicId"].Value);
                cmd.Parameters.Add(para2);

                DbParameter para3 = cmd.CreateParameter();
                para3.Value = (int)FAQActionEnum.AddReply;
                cmd.Parameters.Add(para3);

                conn.Open();

                int count = Convert.ToInt32(cmd.ExecuteScalar());

                if (count > 0)
                {
                    return;
                }

                sql = @"INSERT INTO t_TopicsAboutUser
(UserId,TopicId,Status)
VALUES
(?,?,?)";
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();

                conn.Close();
            }
        }

        public void Conclude(IDataElement topic, Guid eventId, Guid replyId, string conclusion)
        {
            return;
        }

        #endregion

        public IDataElement ListEventAboutCreater(Guid userId, DateTime time)
        {
            using (DbConnection conn = SqlHelper.Connection)
            {
                string sql = @"SELECT t_Events.EventId
FROM t_TopicsAboutUser,t_Events
WHERE t_Events.TargetId=t_TopicsAboutUser.TopicId
AND t_Events.CreateTime > ?
AND t_TopicsAboutUser.UserId=?
AND t_TopicsAboutUser.Status=?
ORDER BY EventNo DESC";

                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;

                DbParameter para1 = cmd.CreateParameter();
                para1.Value = time.ToString();
                cmd.Parameters.Add(para1);

                DbParameter para2 = cmd.CreateParameter();
                para2.Value = DataProviderUtil.GetGuidBase64(userId);
                cmd.Parameters.Add(para2);

                DbParameter para3 = cmd.CreateParameter();
                para3.Value = (int)FAQActionEnum.AddTopic;
                cmd.Parameters.Add(para3);

                conn.Open();

                DbDataReader dr = cmd.ExecuteReader();

                IDataElement de = DataReaderEmit.GetDataElement(dr, "t_Events");

                dr.Close();

                conn.Close();

                return de;

            }
        }

        public IDataElement ListEventAboutReplyer(Guid userId, DateTime time)
        {
            using (DbConnection conn = SqlHelper.Connection)
            {
                string sql = @"SELECT t_Events.EventId
FROM t_TopicsAboutUser,t_Events
WHERE t_Events.TargetId=t_TopicsAboutUser.TopicId
AND t_Events.CreateTime > ?
AND t_TopicsAboutUser.UserId=?
AND t_TopicsAboutUser.Status=?
ORDER BY EventNo DESC";

                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;

                DbParameter para1 = cmd.CreateParameter();
                para1.Value = time.ToString();
                cmd.Parameters.Add(para1);

                DbParameter para2 = cmd.CreateParameter();
                para2.Value = DataProviderUtil.GetGuidBase64(userId);
                cmd.Parameters.Add(para2);

                DbParameter para3 = cmd.CreateParameter();
                para3.Value = (int)FAQActionEnum.AddReply;
                cmd.Parameters.Add(para3);

                conn.Open();

                DbDataReader dr = cmd.ExecuteReader();

                IDataElement de = DataReaderEmit.GetDataElement(dr, "t_Events");

                dr.Close();

                conn.Close();

                return de;

            }
        }
    }
}
